using System.Web;
using System.Web.Mvc;
using OurWedding1.Models;
using System.Linq;

namespace OurWedding1.Controllers
{   
    public class ExpensePhotosController : BaseController
    {
		private readonly IExpenseRepository _expenseRepository;
		private readonly IExpensePhotoRepository _expensephotoRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public ExpensePhotosController() : this(new ExpenseRepository(), new ExpensePhotoRepository())
        {
        }

        public ExpensePhotosController(IExpenseRepository expenseRepository, IExpensePhotoRepository expensephotoRepository)
        {
			_expenseRepository = expenseRepository;
			_expensephotoRepository = expensephotoRepository;
        }

        //
        // GET: /ExpensePhotoes/

        public ActionResult Index(int id)
        {
            var expense = _expenseRepository.Find(id);
            if (!SelectedWeddingId.HasValue)
            {
                RedirectToAction("Index", "Weddings");
            }
            if (expense == null)
            {
                return HttpNotFound();
            }
            if (expense.WeddingId != SelectedWeddingId)
            {
                return HttpNotFound(); //TODO:Error correction
                    
            }
            var model = _expensephotoRepository.All.Where(e => e.ExpenseId == expense.ExpenseId);
            ViewBag.ExpenseId = expense.ExpenseId;
            return View(model);
        }

        //
        // GET: /ExpensePhotoes/Details/5

      

        public ActionResult Upload(int id)
        {
            var selectedWedding = SelectedWeddingId;
            if (!selectedWedding.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            var expense = _expenseRepository.Find(id);
            if (expense == null)
            {
                return RedirectToAction("Index", "Weddings");
            }
// ReSharper disable PossibleInvalidOperationException
            if (expense.WeddingId != SelectedWeddingId.Value)
// ReSharper restore PossibleInvalidOperationException
            {
                return RedirectToAction("Index", "Weddings");
            }
            var model = new ExpensePhoto {ExpenseId = expense.ExpenseId};
            return View(model);
        }

        [HttpPost]
        public ActionResult Upload(ExpensePhoto model,HttpPostedFileBase file)
        {
            var selectedWedding = SelectedWeddingId;
            if (!selectedWedding.HasValue)
            {
                return RedirectToAction("Index", "Weddings");
            }
            if (file.ContentLength > 0)
            {
                var account = new CloudinaryDotNet.Account("hdq73umpz", "694358231294957", "U2rCpJXbmiH2dj8gUxQ2bhI7lFg");

                var cloudinary = new CloudinaryDotNet.Cloudinary(account);
                var uploadParams = new CloudinaryDotNet.Actions.ImageUploadParams
                {
                    File = new CloudinaryDotNet.Actions.FileDescription("dama", file.InputStream)
                };

                CloudinaryDotNet.Actions.ImageUploadResult uploadResult = cloudinary.Upload(uploadParams);
                model.PublicId = uploadResult.PublicId;
                _expensephotoRepository.InsertOrUpdate(model);
                _expensephotoRepository.Save();
                return RedirectToAction("Index", new { id = model.ExpenseId });
            }

            return RedirectToAction("Index");
        }
        

        //
        // GET: /ExpensePhotoes/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(_expensephotoRepository.Find(id));
        }

        //
        // POST: /ExpensePhotoes/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            _expensephotoRepository.Delete(id);
            _expensephotoRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                _expenseRepository.Dispose();
                _expensephotoRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

